package cn.tonyandmoney.voicenotes.ui

import android.os.Bundle
import android.view.Gravity
import cn.tonyandmoney.anko.extend.rightDrawableResId
import cn.tonyandmoney.baidu.ISpeech
import cn.tonyandmoney.voicenotes.ASRActivity
import cn.tonyandmoney.voicenotes.R
import cn.tonyandmoney.voicenotes.alarm.VoiceAlarmActivity
import cn.tonyandmoney.voicenotes.greendao.utils.GreenDaoHelper
import cn.tonyandmoney.voicenotes.notes.enums.OperationEnum
import cn.tonyandmoney.voicenotes.notes.presenter.MainActPresenter
import cn.tonyandmoney.voicenotes.ui.activity.MyFilesActivity
import cn.tonyandmoney.voicenotes.ui.activity.NoteReaderActivity
import cn.tonyandmoney.voicenotes.ui.activity.NotesListActivity
import cn.tonyandmoney.voicenotes.ui.activity.SettingActivity
import com.zlzsy.app.base.ui.BaseActivity
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.jetbrains.anko.*
import org.jetbrains.anko.design.floatingActionButton
import java.util.concurrent.TimeUnit

/**
 * 主界面，各项功能的入口
 * 目前有：
 * 1、语音笔记录入
 * 2、语音笔记记录
 * 3、语音闹钟
 * 4、设置
 * 初始化数据库
 * 语音打开界面功能
 */
class MainActivity : BaseActivity(), MainActPresenter.IMainView {


    private val mPresenter: MainActPresenter by lazy { MainActPresenter(this) }
    private var isForeground: Boolean = false

    override fun initView() {
        frameLayout {
            verticalLayout {
                topPadding = dip(20)
                bottomPadding = dip(20)
                textView {
                    text = "笔记录入"
                    padding = dip(15)
                    textSize = 18f
                    rightDrawableResId = R.drawable.ic_keyboard_arrow_right_black_24dp
                    gravity = Gravity.CENTER_VERTICAL
                    backgroundResource = R.drawable.bg_press_ripper
                    setOnClickListener { startActivity<NoteReaderActivity>() }
                }.lparams(matchParent, wrapContent)
                textView {
                    text = "我的笔记"
                    padding = dip(15)
                    textSize = 18f
                    rightDrawableResId = R.drawable.ic_keyboard_arrow_right_black_24dp
                    gravity = Gravity.CENTER_VERTICAL
                    backgroundResource = R.drawable.bg_press_ripper
                    setOnClickListener { startActivity<NotesListActivity>() }
                }.lparams(matchParent, wrapContent) {
                    topMargin = dip(10)
                }
                textView {
                    text = "我的文件"
                    padding = dip(15)
                    textSize = 18f
                    rightDrawableResId = R.drawable.ic_keyboard_arrow_right_black_24dp
                    gravity = Gravity.CENTER_VERTICAL
                    backgroundResource = R.drawable.bg_press_ripper
                    setOnClickListener { startActivity<MyFilesActivity>() }
                }.lparams(matchParent, wrapContent) {
                    topMargin = dip(10)
                }
                textView {
                    text = "语音闹钟"
                    padding = dip(15)
                    textSize = 18f
                    rightDrawableResId = R.drawable.ic_keyboard_arrow_right_black_24dp
                    gravity = Gravity.CENTER_VERTICAL
                    backgroundResource = R.drawable.bg_press_ripper
                    setOnClickListener { startActivity<VoiceAlarmActivity>() }
                }.lparams(matchParent, wrapContent) {
                    topMargin = dip(10)
                }
                textView {
                    text = "录音设置"
                    padding = dip(15)
                    textSize = 18f
                    rightDrawableResId = R.drawable.ic_keyboard_arrow_right_black_24dp
                    gravity = Gravity.CENTER_VERTICAL
                    backgroundResource = R.drawable.bg_press_ripper
                    setOnClickListener { startActivity<SettingActivity>() }
                }.lparams(matchParent, wrapContent) {
                    topMargin = dip(10)
                }
            }
            floatingActionButton {
                setImageResource(R.drawable.ic_keyboard_voice_black_24dp)
                setOnClickListener { startActivity<ASRActivity>() }
            }.lparams(dip(45), dip(45)) {
                gravity = Gravity.END or Gravity.BOTTOM
                margin = dip(10)
            }
        }
    }

    override fun initData(savedInstanceState: Bundle?) {
        showLoadingDialog(R.string.is_data_init)
        Observable.create<Int> {
            GreenDaoHelper.helper.init(this)
            it.onNext(0);it.onComplete()
        }
                .delay(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    hideLoadingDialog()
                    startActivity<ASRActivity>()
                }
        mPresenter.start()
    }

    override fun onResume() {
        super.onResume()
        isForeground = true
    }

    override fun onPause() {
        super.onPause()
        isForeground = false
    }

    /**
     * 响应操作
     */
    override fun operation(operation: OperationEnum) = operation(operation, null)

    override fun operation(operation: OperationEnum?, params: MutableMap<String, String>?) {
        if (!isForeground) return
        when (operation) {
            OperationEnum.INTENT_NEW_MEMO -> {
                if (checkAsr()) startActivity<NoteReaderActivity>()
            }
            OperationEnum.INTENT_OPEN_FILE -> startActivity<MyFilesActivity>()
            OperationEnum.INTENT_OPEN_NOTE -> startActivity<NotesListActivity>()
            OperationEnum.INTENT_OPEN_ALARM -> startActivity<VoiceAlarmActivity>()
            OperationEnum.INTENT_OPEN_SETTING -> startActivity<SettingActivity>()
            else -> {
            }
        }
    }


    private fun checkAsr(): Boolean {
        if (ISpeech.INSTANCE == null || !ISpeech.INSTANCE!!.isRecognizing) {
            startActivity<ASRActivity>()
            return false
        }
        return true
    }

    override fun onDestroy() {
        ISpeech.INSTANCE?.destroy()
        super.onDestroy()
    }

}
